package base62

import (
	"container/list"
	"math"
	"strings"
)

var base62 string

// Init 初始化62进制字符串
func Init(s string) {
	if len(s) ==0{
		panic("base62 string is empty")
	}
	base62 = s
}

// IntTOString 10进制转62进制
func IntTOString(seq uint64) string {
	if seq == 0 {
		return string(base62[0])
	}
	stack := list.New()
	for seq > 0 {
		yu := seq % 62
		stack.PushBack(base62[yu])
		seq = seq / 62
	}
	res := ""
	for stack.Len() > 0 {
		e := stack.Back()
		stack.Remove(e)
		res += string(e.Value.(byte))
	}
	return res
}

// StringToInt 62进制转10进制
func StringToInt(seq string) (res uint64) {
	len := len(seq)
	for index,tmp := range seq {
		res += uint64(strings.Index(base62,string(tmp))* int(math.Pow(62,float64(len-index-1))))
	}
	return
}
	
